<html>

	<head>
		<meta charset="UTF-8">
		<title>配置</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<link rel="stylesheet" href="../../layui/css/layui.css" />
		<link rel="stylesheet" href="../../css/bass.css" />
		<style>
			html,
			body {
				min-width: 100% !important;
			}
			
			.layui-form-checkbox[lay-skin=primary] {
				height: 30px !important;
			}
			
			.layui-form-checkbox span {
				height: 30px;
			}
			
			body {
				padding: 10px;
				box-sizing: border-box;
			}
			
			#selectTitle {
				height: 90%;
				width: 100%;
				box-sizing: border-box;
				border: 1px solid #e2dcdc;
			}
			
			::-webkit-scrollbar {
				display: none;
			}
		</style>
	</head>

	<body style="margin-bottom:0 !important;height: 100%;">
		<!--配置-->
		<div id="selectTitle" class="list-select">
			<!--左侧div-->
			<div style="height: 100%;overflow: auto;width:30%;float:left;box-sizing:border-box;">
				<ul id="treeTop" style="color: black;margin-bottom: 20px;"></ul>
				<ul id="treeBottom" style="color: black;"></ul>
			</div>
			<!--右侧div-->
			<div style="border:1px solid #e2dcdc;width:70%;height: 100%;float:left;box-sizing: border-box;border-right: none;border-bottom: none;border-top: none;">
				<div class="left-box">
					<div class="left-box-top center">未关联</div>
					<div class="left-box-bottom">
						<ul id="noRelation"></ul>
					</div>
				</div>
				<div class="center-box">
					<div style="width:100%;"></div>
					<div style="width:100%;margin-top: 100%;">
						<button id="add-one" title="添加选中项"> &gt; </button>
						<button id="add-all" title="添加全部"> &gt;&gt; </button>
						<button id="remove-one" title="移除选中项"> &lt; </button>
						<button id="remove-all" title="移除全部"> &lt;&lt; </button>
					</div>
				</div>
				<div class="right-box">
					<div class="right-box-top center">已关联</div>
					<div class="right-box-bottom">
						<ul id="relation"></ul>
					</div>
				</div>
			</div>
			<button class="layui-btn layui-btn-blue search layui-btn-sm" id="addSiteConfig" lay-filter="search" style="position: fixed;bottom: 10px;right: 10px;">提交</button>
		</div>
		<!--配置-->
		<script src="../../js/common/jquery-1.11.3.min.js"></script>
		<script src="../../layui/layui.js"></script>
		<script src="../../layui/lay/modules/tree.js"></script>
		<script src="../../js/common/navList.js"></script>
		<script src="../../js/common/ajax.js"></script>
		<script src="../../js/common/data.js"></script>
		<script src="../../js/common/list-move.js"></script>
		<script>
			//			console.log(parent.layer.getFrameIndex(window.name));
			var href = window.location.href;
			console.log("---" + href);
			var reportIndex = href.split("?")[1].split("=")[1];
			console.log("reportIndex=" + reportIndex);

			layui.use(['tree', 'layer'], function() {
				var layer = layui.layer,
					$ = layui.jquery;
				//请求管理站(全部站点=合计（合计就是已关联）+为管理啊)
				$.ajax({
					url: baseUrl + "/site/getListNoPage",
					type: "GET",
					async: false,
					success: function(data) {
						console.log(data);
						if(data.code == 200) {
							var list = data.list;
							//							administrate = list;
							if(list.length != 0) {
								var children = "[";
								for(var i = 0; i < list.length; i++) {
									//									administrate[0].push(list[i].siteName);
									if(i == list.length - 1) {
										children += "{name:'" + list[i].siteName + "'}]";
									} else {
										children += "{name:'" + list[i].siteName + "'},";
									}
								}
							};
							children = (new Function("return " + children))();

							layui.tree({
								elem: '#treeTop', //传入元素选择器
								nodes: [{ //节点
									name: '管理站',
									spread: true, //展开
									children: children //必须是个json
								}],
								click: function(node) {
									console.log(node) //node即为当前点击的节点数据
								}
							});

						} else {
							layer.msg("管理站数据请求失败", {
								icon: 5
							});
						}
					}
				});

				heji();
				//请求合计
				function heji() {
					$.ajax({
						url: baseUrl + "/monitordata/getSiteConfigList",
						type: "POST",
						data: {
							"reportIndex": reportIndex
						},
						async: false,
						success: function(data) {
							if(data.length != 0) {
								var children = "[";
								for(var i = 0; i < data.length; i++) {
									//									administrate[2].push(data[i].siteName);
									$("#relation").append("<li class='m5' siteId=" + data[i].id + ">" + data[i].siteName + "</li>");
									if(i == data.length - 1) {
										children += "{name:'" + data[i].siteName + "'}]";
									} else {
										children += "{name:'" + data[i].siteName + "'},";
									}
								}
							};
							children = (new Function("return " + children))()

							layui.tree({
								elem: '#treeBottom', //传入元素选择器
								nodes: [{ //节点
									name: '合计',
									spread: true, //展开
									children: children //必须是个json
								}],
								click: function(node) {
									console.log(node) //node即为当前点击的节点数据
								}
							});

						}
					});
				}
				//请求未关联
				$.ajax({
					url: baseUrl + "/monitordata/getSiteNotConfigList",
					type: "POST",
					data: {
						"reportIndex": reportIndex
					},
					async: false,
					success: function(data) {
						console.log(data);

						$("#noRelation").html(); //清空内容
						for(var i = 0; i < data.length; i++) {
							var item = data[i];
							$("#noRelation").append("<li class='m5' siteid=" + item.id + ">" + item.siteName + "</li>");
						}
					}
				});
				//提交数据
				$("#addSiteConfig").click(function() {
					layer.msg("数据提交成功", {
						icon: 6
					});
					setTimeout(function() {
						var mylay = parent.layer.getFrameIndex(window.name);
						parent.layer.close(mylay);
					}, 1000);
					//					//循环已关联模块中的数据
					//					var siteNames = [];
					//					$("#relation li").each(function() {
					//						siteNames.push($(this).text());
					//					});

					//					if(siteNames.length == 0) {
					//						layer.msg("提交不能为空", {
					//							icon: 5
					//						});
					//						return
					//					}
					//					$.ajax({
					//						url: baseUrl + "/monitordata/addSiteConfig",
					//						type: "POST",
					//						data: {
					//							"siteNames": siteNames,
					//							"reportIndex": reportIndex,
					//						},
					//						async: false,
					//						success: function(data) {
					//							console.log(data);
					//							if(data.code == 200) {
					//								$("treeBottom").remove(); //清除原先合计
					//								heji(); //刷新合计
					//								layer.msg("数据提交成功", {
					//									icon: 5
					//								});
					//								
					//							} else {
					//								parent.layer.close(mylay);
					//								layer.msg("数据提交失败", {
					//									icon: 5
					//								});
					//							}
					//						}
					//					});
				});

			});
		</script>
	</body>

</html>